package org.gzc.controller;

import javax.servlet.http.HttpSession;

import org.gzc.exception.LoginException;
import org.gzc.exception.RegisterException;
import org.gzc.pojo.User;
import org.gzc.service.UserService;
import org.gzc.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	/**
     * 处理用户登录
     *
     * @param user
     * @param verCode
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    @ResponseBody
    public ResultUtil<String> login(User user, String verCode, HttpSession session) throws Exception {
        String vCode = (String) session.getAttribute("verCode");
        if (!verCode.equals(vCode)) { // 验证码错误
            throw new LoginException("验证码错误！");
        }
        userService.Login(user, session);
        session.removeAttribute("verCode"); // 验证码失效
        return ResultUtil.success.msg("登录成功");
    }

    /**
     * 处理用户注册
     *
     * @param user
     * @param verCode
     * @param model
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/register.do", method = RequestMethod.POST)
    @ResponseBody
    public ResultUtil<String> register(User user, String verCode, Model model, HttpSession session) throws Exception {
        String vCode = (String) session.getAttribute("verCode");
        if (!verCode.equals(vCode)) { // 验证码错误
            throw new RegisterException("验证码错误！");
        }
        userService.register(user);
        session.removeAttribute("verCode"); // 验证码失效
        return ResultUtil.success.msg("注册成功");
    }


	/**
	 * 用户登录退出
	 *
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "login";
	}

    /**
     * 添加新用户
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultUtil<String> addUser(User user) {
        if (userService.addUser(user) > 0) {
            return ResultUtil.success.msg("添加成功");
        }
        return ResultUtil.failed.msg("增加失败！");
    }

    /**
     * 删除用户
     *
     * @param users
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultUtil<String> deleteUser(@RequestBody User[] users) throws Exception {
        List<User> userList = new ArrayList<>();
        for (User user : users) {
            userList.add(user);
        }
        if (userService.deleteUsers(userList) > 0) {
            return ResultUtil.success.msg("删除成功！");
        }
        return ResultUtil.failed.msg("删除失败！");
    }

    /**
     * 更新用户
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultUtil<String> updateUser(User user) {
        if (userService.updateUser(user) > 0) {
            return ResultUtil.success.msg("更新成功！");
        }
        return ResultUtil.failed.msg("更新失败！");
	}



}
